const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class Promise {
    constructor(task) {
        this.state = PENDING // 初始状态
        this.value = undefined // 成功的结果
        this.reason = undefined // 失败的结果
        this.onFulfilledCallbacks = [] // 成功的回调列表
        this.onRejectedCallbacks = [] // 失败的回调列表
        // 修改状态为成功
        const resolve = (value) => {
            if (this.state === PENDING) { // 状态只能修改一次
                this.state = FULFILLED
                this.value = value
                this.onFulfilledCallbacks.forEach(cb => cb(this.value)) // 执行所有成功状态的回调
            }
        }
        // 修改状态为失败
        const reject = (reason) => {
            if (this.state === PENDING) { // 状态只能修改一次
                this.state = REJECTED
                this.reason = reason
                this.onRejectedCallbacks.forEach(cb => cb(this.value)) // 执行所有失败状态的回调
            }
        }
        try {
            task(resolve, reject)
        } catch (e) {
            reject(e)
        }
    }
    then (onFulfilled, onRejected) {
        return new Promise((resolve, reject) => {
            // 成功状态 - 值传递
            const Fulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value
            // 失败状态 - 异常传递
            const Rejected = typeof onRejected === 'function' ? onRejected : reason => throw reason
            if (this.state === PENDING) {
                this.onFulfilledCallbacks.push(() => {
                    try {
                      // this.value 是执行时的this.value
                      const result = Fulfilled(this.value)
                        resolve(result)
                    } catch (e) {
                        reject(e)
                    }
                })
                this.onRejectedCallbacks.push(() => {
                    try {
                        const result = Rejected(this.reason)
                        resolve(result)
                    } catch (e) {
                        reject(e)
                    }
                })
            }
            if (this.state === FULFILLED) {
                try {
                    const result = Fulfilled(this.value)
                    resolve(result)
                } catch (e) {
                    reject(e)
                }
            }
            if (this.state === REJECTED) {
                try {
                    const result = Rejected(this.reason)
                    resolve(result)
                } catch (e) {
                    reject(e)
                }
            }
        })
    }
    catch (onRejected) {
      this.then(null, onRejected)
    }
    static all (promises) {
      return new Promise(resolve => {
        let resultList = [], count = 0
        promises.forEach((promiseItem, i) => {
          // 外面包装一层Promise.resolve为了调用它的then方法
          // 因为不包装的话promiseItem的执行结果有可能是状态
          // then之所以也能捕捉错误，是因为then的实现：被拒绝是错误传递，否则就是值传递
          Promise.resolve(promiseItem).then(res => {
            resultList[i] = res
            // 每个任务执行万都会检查count是不是与promises.length相等，如果相等，说明所有任务执行完成且成功
            // count++
            if (resultList.length === promises.length) resolve(arr)
          })
        })
      })
    }
    static race (promises) {
      return new Promise(resolve => {
        for (const promiseItem of promises) {
          // 只要有一个成功就会马上返回
          Promise.resolve(promiseItem).then(resolve)
        }
      })
    }
    // 所有的promise失败时Promise.any才失败
    static any (promises) {}
}
